/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logic;

import classes.*;
import database.database;
import java.util.ArrayList;

/**
 *
 * @author Shelling Ford
 */
public class Main {
    // attribut

    public static ArrayList<User> userList = new ArrayList<User>();
    public static ArrayList<Room> roomList = new ArrayList<Room>();
    public static ArrayList<Message> messageList = new ArrayList<Message>();
    public static ArrayList<Category> categoryList = new ArrayList<Category>();

    // inisialisasi sekali aja, kalau server di reset, dipanggil di fungsi init halaman login
    public static void init() {
        // isi userList, categoryList dari database
        database db = new database();
        Main.userList = db.getAllUser();

        Room home = new Room("home",-1,Main.userList.get(0));
        home.userInRoom = userList;
        Main.roomList.add(home);

        Room d1 = new Room("botRoom1",1,Main.userList.get(0));
        Room d2 = new Room("botRoom2",2,Main.userList.get(0));
        Room d3 = new Room("botRoom3",3,Main.userList.get(0));
        Main.roomList.add(d1);
        Main.roomList.add(d2);
        Main.roomList.add(d3);
    }

    // mengambil user dari username
    public static User getUserFromName(String username) {
        for (int i = 0; i < Main.userList.size(); i++) {
            if (Main.userList.get(i).username.equalsIgnoreCase(username)) {
                return Main.userList.get(i);
            }
        }
        return null;
    }

    // mengambil room dari roomname
    public static Room getRoomFromName(String roomname) {
        for (int i = 0; i < Main.roomList.size(); i++) {
            if (Main.roomList.get(i).name.equalsIgnoreCase(roomname)) {
                return Main.roomList.get(i);
            }
        }
        return null;
    }

    public static ArrayList<Room> getRoomInCategory(int catId) {
        ArrayList<Room> retVal = new ArrayList<Room>();
        for (int i = 0; i < Main.roomList.size(); i++) {
            if (Main.roomList.get(i).place == catId) {
                retVal.add(Main.roomList.get(i));
            }
        }
        System.out.println("ketemu room"+retVal.size());
        return retVal;
    }

    // mengambil message untuk user yang sedang login
    public static ArrayList<Message> getMessage(User user) {
        int lastId = user.lastMessageId;
        ArrayList<Message> retVal = new ArrayList<Message>();
        for (int i = 0; i < Main.messageList.size(); i++) {
            if (Main.messageList.get(i).recepientUser != null) {
                if (Main.messageList.get(i).recepientUser == user && Main.messageList.get(i).id > user.lastMessageId) {
                    retVal.add(Main.messageList.get(i));
                    lastId = Main.messageList.get(i).id;
                }
            } else {
                if (Main.messageList.get(i).recepientRoom.userInRoom.contains(user) && Main.messageList.get(i).id > user.lastMessageId) {
                    retVal.add(Main.messageList.get(i));
                    lastId = Main.messageList.get(i).id;
                }
            }
        }
        user.lastMessageId = lastId;
        return retVal;
    }

    // menambah message ke dalam list, id-nya diisi dulu dengan yang terakhir
    public static void addMessage(Message message) {
        if (Main.messageList.isEmpty()) {
            message.id = 0;
        } else {
            message.id = Main.messageList.get(Main.messageList.size() - 1).id + 1;
        }
        Main.messageList.add(message);
    }

    public static int getOwnedRoom(User us) {
        int count = 0;
        for (int i=0;i<Main.roomList.size();i++) {
            if (Main.roomList.get(i).owner == us) {
                count++;
            }
        }
        return count;
    }
}
